For this assignment, build the first letters for both your first and last name using point plots in R.
Then, write R code that will left multiply (%>%) a square matrix (x) against each of the vectors of points (y). Initially, that square matrix will be the Identity matrix.
Use a loop that changes the transformation matrix incrementally to demonstrate 1) shear, 2) scaling, 3) rotation, and 4) projection in animated fashion.
# PT
x=c(rep(-1,500), seq(-1,0,length.out=1000), seq(-1,0,length.out=1000),
rep(0,250), seq(0.25,.75,length.out=500), rep(.75,1000), seq(.75,1.25,length.out=500))
y=c(seq(-1,1,length.out=500), rep(0,1000), rep(1,1000), seq(0,1,length.out=250),
rep(1,500), seq(-1,1,length.out=1000), rep(1,500))
# T
#x=c(seq(0,.5,length.out=500), rep(.5,1000), seq(.5,1,length.out=500))
#y=c(rep(1,500), seq(-1,1,length.out=1000), rep(1,500))
# H (from prompt)
#x=c(rep(0,500), seq(0,1,length.out=1000), rep(1,500))
#y=c(seq(-1,1,length.out=500), rep(0,1000), seq(-1,1,length.out=500))
z=rbind(x,y)
# Confirm the 'PT' is showing based on prompt code
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))
# Create Identify Matrix
trans_matrix <- diag(2)
# Multiply Identity matrix by the vector of points
# Realistically, the base identity matrix does not modify the vector of points
result <- trans_matrix %*% z
x11(title="Animation")
#result
# Display resulting plot, should be same as the initial plot
plot(result[2,]~result[1,], xlim=c(-10,10), ylim=c(-10,10))
# Add sleep so the animation will be easier to watch
Sys.sleep(1)
# Create matrix for shear
trans_matrix <- matrix(
c(1, 0, 1.2, 1), nrow=2, ncol=2)
result <- trans_matrix %*% z
# Display resulting plot, should be shear plot
plot(result[2,]~result[1,], xlim=c(-10,10), ylim=c(-10,10))
Sys.sleep(1)
# Create matrix for scale
trans_matrix <- matrix(
c(2, 0, 0, 2), nrow=2, ncol=2)
result <- trans_matrix %*% result
# Display resulting plot, should be scale plot
plot(result[2,]~result[1,], xlim=c(-10,10), ylim=c(-10,10))
Sys.sleep(1)
# Create matrix for rotation (90 degrees counterclockwise)
trans_matrix <- matrix(
c(0, 1, -1, 0), nrow=2, ncol=2)
result <- trans_matrix %*% result
# Display resulting plot, should be rotate plot
plot(result[2,]~result[1,], xlim=c(-10,10), ylim=c(-10,10))
Sys.sleep(1)
# Create matrix for projection (projection on the y axis)
trans_matrix <- matrix(
c(0, 1, 0, 1), nrow=2, ncol=2)
result <- trans_matrix %*% result
# Display resulting plot, should be projection plot
plot(result[2,]~result[1,], xlim=c(-10,10), ylim=c(-10,10))
# PT
x=c(rep(-1,500), seq(-1,0,length.out=1000), seq(-1,0,length.out=1000),
rep(0,250), seq(0.25,.75,length.out=500), rep(.75,1000), seq(.75,1.25,length.out=500))
y=c(seq(-1,1,length.out=500), rep(0,1000), rep(1,1000), seq(0,1,length.out=250),
rep(1,500), seq(-1,1,length.out=1000), rep(1,500))
z=rbind(x,y)
# Confirm the 'PT' is showing based on prompt code
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))
# Create Identity Matrix
id_matrix <- diag(2)
# Create matrix for shear
shear_matrix <- matrix(c(1, 0, 1.2, 1), nrow=2, ncol=2)
# Create matrix for scale
scale_matrix <- matrix(c(2, 0, 0, 2), nrow=2, ncol=2)
# Create matrix for rotation (90 degrees counterclockwise)
rot_matrix <- matrix(c(0, 1, -1, 0), nrow=2, ncol=2)
# Create matrix for projection (projection on the y axis)
# Projection of 2D to 1D
proj_matrix <- matrix(c(0, 1, 0, 1), nrow=2, ncol=2)
# Make list of the transformation matrices to allow for looping
transforms <- list(id_matrix, shear_matrix, scale_matrix, rot_matrix, proj_matrix)
# Use x11 for Animation
x11(title="Animation")
# Loop the transformation matrices
# 5 displays total
for (idx in 1:5) {
# Matrix multiple (apply transformation)
z <- transforms[[idx]] %*% z
# Display resulting plot
plot(z[2,]~z[1,], xlim=c(-10,10), ylim=c(-10,10))
# Add sleep so the animation will be easier to watch
Sys.sleep(0.5)
}
library(animation)
# PT
x=c(rep(-1,500), seq(-1,0,length.out=1000), seq(-1,0,length.out=1000),
rep(0,250), seq(0.25,.75,length.out=500), rep(.75,1000), seq(.75,1.25,length.out=500))
y=c(seq(-1,1,length.out=500), rep(0,1000), rep(1,1000), seq(0,1,length.out=250),
rep(1,500), seq(-1,1,length.out=1000), rep(1,500))
# H (from prompt)
#x=c(rep(0,500), seq(0,1,length.out=1000), rep(1,500))
#y=c(seq(-1,1,length.out=500), rep(0,1000), seq(-1,1,length.out=500))
z=rbind(x,y)
# Confirm the 'PT' is showing based on prompt code
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))
dev.control('enable')
anim = ani.record(reset = T, replay.cur = F)
# Create display window for animation
x11()
# Create 2x2 identity matrix
a=diag(2)
# Scale
for (i in seq(-3,3, length.out=20)) {
a[1,1] = i
a[2,2] = i
mod_mat = apply(z, 2, function(x) a%*%x)
plot(mod_mat[2,]~mod_mat[1,], xlim=c(-3,3), ylim=c(-3,3))
ani.record()
}
# Create 2x2 identity matrix
a=diag(2)
# Shear
for (i in seq(-3,3, length.out=20)) {
a[1,2] = i
mod_mat = apply(z, 2, function(x) a%*%x)
plot(mod_mat[2,]~mod_mat[1,], xlim=c(-3,3), ylim=c(-3,3))
ani.record()
}
# Create 2x2 identity matrix
a=diag(2)
# Project on X axis (while X value scales)
for (i in seq(-3,3, length.out=100)) {
# a[1,1] = i
a[2,2] = i
mod_mat = apply(z, 2, function(x) a%*%x)
plot(mod_mat[2,]~mod_mat[1,], xlim=c(-3,3), ylim=c(-3,3))
ani.record()
}
rotate_func=function(x){matrix(c(cos(x), -sin(x), sin(x), cos(x)), byrow=T, nrow=2)}
# Create 2x2 identity matrix
a=diag(2)
# Rotate
for (i in seq(0,2, length.out=20)) {
a=rotate_func(i)
mod_mat = apply(z, 2, function(x) a%*%x)
plot(mod_mat[2,]~mod_mat[1,], xlim=c(-3,3), ylim=c(-3,3))
ani.record()
}